#!/usr/bin/env python
# -*- encoding: utf-8 -*-

import pandas as pd

from xpy3lib.XRetryableQuery import XRetryableQuery
from xpy3lib.XRetryableSave import XRetryableSave
from sicost.AbstractDPJob import AbstractDPJob


class DeleteRDSJob(AbstractDPJob):
    """
    通过两个SQL去取来源表和目标表的表结构

    传入6个参数   来源标  和目标表  及开始时间结束时间

    将来源表那段时间的数据取出来  在存到目标表里
    """
    source_schema = None
    source_table = None


    def __init__(self,
                 p_config=None,
                 p_db_conn_rds=None,
                 p_source_schema=None,
                 p_source_table=None,
                 p_account_period_end=None):

        super(DeleteRDSJob, self).__init__(p_config=p_config,
                                           p_db_conn_mpp=None,
                                           p_db_conn_rds=p_db_conn_rds,
                                           p_db_conn_dbprod7=None,
                                           p_unit=None,
                                           p_account=None,
                                           p_cost_center=None,
                                           p_account_period_start=None,
                                           p_account_period_end=p_account_period_end)

        self.source_schema = p_source_schema
        self.source_table = p_source_table

        pass

    def do_execute(self):
        """
        """
        self.logger.info('DeleteRDSJob.do_execute')
        sql = " SELECT COLNAME FROM " \
              " SYSCAT.columns" \
              " WHERE 1=1 " \
              " AND TABSCHEMA  = '%s' AND TABNAME= '%s'" % (
                  self.source_schema, self.source_table)

        self.logger.info(sql)

        df1 = XRetryableQuery(p_db_conn=self.db_conn_rds, p_sql=sql, p_max_times=5).redo()
        success = df1.empty is False
        if success is False:
            return
        df1.columns = df1.columns.str.upper()
        self.logger.info('df1_properties-')
        # df1_properties = df1.columns.values
        df1_properties = df1['COLNAME'].values.tolist()
        self.logger.info(",".join(df1_properties))
        # account_period_start = self.account_period_start.astype(str)
        # account_period_end = self.account_period_end.astype(str)
        # account_period_start = self.account_period_start.strftime('%Y%m%tmp_dict%H%M%S')
        # account_period_end = self.account_period_end.strftime('%Y%m%tmp_dict%H%M%S')
        if 'PRODUCE_TIME' in df1_properties:
            time_field = 'PRODUCE_TIME'
            account_period_end = self.account_period_end
        else:
            time_field = 'DATE'
            account_period_end = self.account_period_end[0:8]

        sql = " SELECT * FROM " \
              " %s.%s" \
              " WHERE 1=1 " \
              " AND %s < '%s'" % (
                  self.source_schema, self.source_table, time_field,
                  account_period_end)

        self.logger.info(sql)

        df0 = XRetryableQuery(p_db_conn=self.db_conn_rds, p_sql=sql, p_max_times=5).redo()
        success = df0.empty is False
        if success is False:
            return
        df0.columns = df0.columns.str.upper()




        sql = " DELETE FROM " \
              " %s.%s" \
              " WHERE 1=1 " \
              " AND %s < '%s'" % (
                  self.source_schema, self.source_table, time_field,
                  account_period_end)

        self.logger.info(sql)
        self.db_conn_rds.execute(sql)







        super(DeleteRDSJob, self).do_execute()
